<template>
    <div class="body" v-if="application.parentId != null">
        <div class="tit">开具发票申请单</div>
        <el-form :model="applicationOld" :rules="rules" ref="newform" inline-message>
            <el-container>
                <el-header>
                    <el-row>
                        <el-col :span="4">
                            申请部门
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input v-model="applicationOld.applicationDepartment" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            申请人
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input v-model="applicationOld.applyPerson" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            申请时间
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input v-model="applicationOld.applyTime" readonly></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="4">
                            <div>
                                <span style="color: red;">*</span><span>业务类型</span>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="businessType">
                                <el-select v-model="applicationOld.businessType" disabled allow-create filterable
                                    default-first-option>
                                    <el-option v-for="item in typeOptions" :label="item.label"
                                        :value="item.value"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <div>
                                <span style="color: red;">*</span><span>发票类别</span>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="type">
                                <el-select v-model="applicationOld.type" disabled>
                                    <el-option label="内销-增值税专用发票" value="内销-增值税专用发票"></el-option>
                                    <el-option label="内销-增值税普通发票" value="内销-增值税普通发票"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="4">
                            <div>
                                <span style="color: red;">*</span><span>合同号</span>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="contractNo">
                                <el-input v-model="applicationOld.contractNo" disabled></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <div>
                                <span style="color: red;">*</span><span>计划回款时间</span>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="planPayTime">
                                <el-date-picker v-model="applicationOld.planPayTime" type="date"
                                    placeholder="Pick a Date" format="YYYY/MM/DD" disabled />
                            </el-form-item>
                        </el-col>
                    </el-row>

                </el-header>
                <el-container>
                    <el-col :span="2">
                        <el-aside>
                            发票信息
                        </el-aside>
                    </el-col>
                    <el-main>
                        <el-row>
                            <el-col :span="3">
                                <div>
                                    <span style="color: red;">*</span><span>客户名称</span>
                                </div>
                            </el-col>
                            <el-col :span="21">
                                <el-form-item prop="customerName">
                                    <el-select v-model="applicationOld.customerName" filterable remote reserve-keyword
                                        placeholder="请选择" @change="changeCustomerName" :remote-method="searchCustomer"
                                        :loading="loading">
                                        <el-option v-for="item in customerNameOptions" :key="item.id" :label="item.name"
                                            :value="item.name" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="3">
                                地址
                            </el-col>
                            <el-col :span="21">
                                <el-form-item>
                                    <el-input v-model="applicationOld.customerAddress" readonly></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="3">
                                纳税识别号
                            </el-col>
                            <el-col :span="21">
                                <el-form-item>
                                    <el-input v-model="applicationOld.customerPayNumber" readonly></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row v-if="applicationOld.businessType == '材料' || applicationOld.businessType == '废料'">
                            <el-col :span="3">
                                订单号
                            </el-col>
                            <el-col :span="21">
                                <el-form-item>
                                    <el-input v-model="application.orderNumber" readonly></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row class="title"
                            v-if="applicationOld.businessType == '箱类' || applicationOld.businessType == '材料'">
                            <el-col :span="3">
                                商品名称
                            </el-col>
                            <el-col :span="4">
                                规格型号
                            </el-col>
                            <el-col :span="4">
                                单位
                            </el-col>
                            <el-col :span="4">
                                数量
                            </el-col>
                            <el-col :span="4">
                                含税单价
                            </el-col>
                            <el-col :span="5">
                                含税金额
                            </el-col>
                        </el-row>
                        <el-row class="title" v-if="applicationOld.businessType == '箱类'"
                            v-for="item in applicationOld.list">
                            <el-col :span="3">
                                <el-form-item>
                                    <el-input placeholder="请输入名称" v-model="item.commodityNo" disabled></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入型号" v-model="item.specification" disabled></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <!-- <el-select v-model="item.unit" disabled>
                                        <el-option value="CNY" label="CNY"></el-option>
                                        <el-option value="USD" label="USD"></el-option>
                                    </el-select> -->
                                    <el-input placeholder="请输入单位" v-model="item.unit" disabled></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入数量" v-model="item.amount" @change="changeMoney"
                                        disabled></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入含税单价" v-model="item.unitPriceIncludingTax"
                                        @change="changeMoney" disabled></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-input v-model="item.amountIncludingTax" readonly></el-input>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="3">
                                增值税发票需特殊备注信息
                            </el-col>
                            <el-col :span="21">
                                <el-form-item>
                                    <el-input v-model="applicationOld.remark" disabled></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-main>
                </el-container>
            </el-container>
        </el-form>
    </div>
    <div class="body">
        <div class="tit">开具发票申请单</div>
        <el-form :model="application" :rules="rules" ref="newform" inline-message>
            <el-container>
                <el-header>
                    <el-row>
                        <el-col :span="4">
                            申请部门
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input v-model="application.applicationDepartment" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            申请人
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input v-model="application.applyPerson" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            申请时间
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input v-model="application.applyTime" readonly></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="4">
                            <div>
                                <span style="color: red;">*</span><span>业务类型</span>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="businessType">
                                <el-select v-model="application.businessType" :disabled="use_store.operation == 1"
                                    allow-create filterable default-first-option>
                                    <el-option v-for="item in typeOptions" :label="item.label"
                                        :value="item.value"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <div>
                                <span style="color: red;">*</span><span>发票类别</span>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="type">
                                <el-select v-model="application.type" :disabled="use_store.operation == 1">
                                    <el-option label="内销-增值税专用发票" value="内销-增值税专用发票"></el-option>
                                    <el-option label="内销-增值税普通发票" value="内销-增值税普通发票"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="4">
                            <div>
                                <span style="color: red;">*</span><span>合同号</span>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item :prop="application.businessType == '材料' ? 'contractNo1' : 'contractNo'">
                                <el-input v-model="application.contractNo"
                                    :disabled="use_store.operation == 1"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <div>
                                <span style="color: red;">*</span><span>计划回款时间</span>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item prop="planPayTime">
                                <el-date-picker v-model="application.planPayTime" type="date" placeholder="Pick a Date"
                                    format="YYYY/MM/DD" :disabled="use_store.operation == 1" />
                            </el-form-item>
                        </el-col>
                    </el-row>

                </el-header>
                <el-container>
                    <el-col :span="2">
                        <el-aside>
                            发票信息
                        </el-aside>
                    </el-col>
                    <el-main>
                        <el-row>
                            <el-col :span="3">
                                <div>
                                    <span style="color: red;">*</span><span>客户名称</span>
                                </div>
                            </el-col>
                            <el-col :span="21">
                                <el-form-item prop="customerName">
                                    <el-select v-model="application.customerName" filterable remote reserve-keyword
                                        placeholder="请选择" @change="changeCustomerName" :remote-method="searchCustomer"
                                        :loading="loading">
                                        <el-option v-for="item in customerNameOptions" :key="item.id" :label="item.name"
                                            :value="item.name" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="3">
                                地址
                            </el-col>
                            <el-col :span="21">
                                <el-form-item>
                                    <el-input v-model="application.customerAddress" readonly></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="3">
                                纳税识别号
                            </el-col>
                            <el-col :span="21">
                                <el-form-item>
                                    <el-input v-model="application.customerPayNumber" readonly></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row v-if="application.businessType == '材料' || application.businessType == '废料'">
                            <el-col :span="3">
                                <div>
                                    <span style="color: red;">*</span><span>订单号</span>
                                </div>
                            </el-col>
                            <el-col :span="21">
                                <el-form-item prop="orderNumber">
                                    <el-input v-model="application.orderNumber"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row class="title"
                            v-if="application.businessType == '箱类' || application.businessType == '材料'">
                            <el-col :span="3">
                                商品名称
                            </el-col>
                            <el-col :span="4">
                                规格型号
                            </el-col>
                            <el-col :span="4">
                                单位
                            </el-col>
                            <el-col :span="4">
                                数量
                            </el-col>
                            <el-col :span="4">
                                含税单价
                            </el-col>
                            <el-col :span="5">
                                含税金额
                            </el-col>
                        </el-row>
                        <el-row class="title"
                            v-if="application.businessType == '箱类' || application.businessType == '材料'">
                            <el-col :span="3">
                                <el-form-item prop="list.0.commodityNo"
                                    :rules="application.businessType == '材料' ? { required: false, trigger: 'change' } : { required: true, message: '请填写', trigger: 'change' }">
                                    <el-input placeholder="请输入名称" v-model="application.list[0].commodityNo"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item prop="list.0.specification"
                                    :rules="application.businessType == '材料' ? { required: false, trigger: 'change' } : { required: true, message: '请填写', trigger: 'change' }">
                                    <el-input placeholder="请输入型号" v-model="application.list[0].specification"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item prop="list.0.unit"
                                    :rules="application.businessType == '材料' ? { required: false, trigger: 'change' } : { required: true, message: '请填写', trigger: 'change' }">
                                    <!-- <el-select v-model="item.unit" :disabled="use_store.operation == 1">
                                        <el-option value="CNY" label="CNY"></el-option>
                                        <el-option value="USD" label="USD"></el-option>
                                    </el-select> -->
                                    <el-input placeholder="请输入单位" v-model="application.list[0].unit"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item prop="list.0.amount"
                                    :rules="application.businessType == '材料' ? { required: false, trigger: 'change' } : { required: true, message: '请填写', trigger: 'change' }">
                                    <el-input placeholder="请输入数量" v-model="application.list[0].amount"
                                        @change="changeMoney" :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item prop="list.0.unitPriceIncludingTax"
                                    :rules="application.businessType == '材料' ? { required: false, trigger: 'change' } : { required: true, message: '请填写', trigger: 'change' }">
                                    <el-input placeholder="请输入含税单价" v-model="application.list[0].unitPriceIncludingTax"
                                        @change="changeMoney" :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-input v-model="application.list[0].amountIncludingTax" readonly></el-input>
                            </el-col>
                        </el-row>
                        <el-row class="title"
                            v-if="application.businessType == '箱类' || application.businessType == '材料'">
                            <el-col :span="3">
                                <el-form-item>
                                    <el-input placeholder="请输入名称" v-model="application.list[1].commodityNo"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入型号" v-model="application.list[1].specification"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <!-- <el-select v-model="item.unit" :disabled="use_store.operation == 1">
                                        <el-option value="CNY" label="CNY"></el-option>
                                        <el-option value="USD" label="USD"></el-option>
                                    </el-select> -->
                                    <el-input placeholder="请输入单位" v-model="application.list[1].unit"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入数量" v-model="application.list[1].amount"
                                        @change="changeMoney" :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入含税单价" v-model="application.list[1].unitPriceIncludingTax"
                                        @change="changeMoney" :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-input v-model="application.list[1].amountIncludingTax" readonly></el-input>
                            </el-col>
                        </el-row>
                        <el-row class="title"
                            v-if="application.businessType == '箱类' || application.businessType == '材料'">
                            <el-col :span="3">
                                <el-form-item>
                                    <el-input placeholder="请输入名称" v-model="application.list[2].commodityNo"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入型号" v-model="application.list[2].specification"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <!-- <el-select v-model="item.unit" :disabled="use_store.operation == 1">
                                        <el-option value="CNY" label="CNY"></el-option>
                                        <el-option value="USD" label="USD"></el-option>
                                    </el-select> -->
                                    <el-input placeholder="请输入单位" v-model="application.list[2].unit"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入数量" v-model="application.list[2].amount"
                                        @change="changeMoney" :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="4">
                                <el-form-item>
                                    <el-input placeholder="请输入含税单价" v-model="application.list[2].unitPriceIncludingTax"
                                        @change="changeMoney" :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="5">
                                <el-input v-model="application.list[2].amountIncludingTax" readonly></el-input>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="3">
                                增值税发票需特殊备注信息
                            </el-col>
                            <el-col :span="21">
                                <el-form-item>
                                    <el-input v-model="application.remark"
                                        :disabled="use_store.operation == 1"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="3">
                                <div>
                                    <span style="color: red;"
                                        v-if="application.businessType != '材料' && application.businessType != '废料'">*</span><span>合同</span>
                                </div>
                            </el-col>
                            <el-col :span="21">
                                <el-form-item prop="fileList" class="upLoad">
                                    <el-upload type="primary" v-model:file-list="fileList" :action="`api/files/upload/`"
                                        :headers="headers" :on-success="onSuccess" :on-change="handleChangePic"
                                        :show-file-list="false">
                                        <el-button>上传</el-button>
                                    </el-upload>
                                    <el-button style="margin-left: 20px;" @click="donwload"
                                        :disabled="application.file == ''">
                                        {{ application.fileName ? application.fileName : '下载' }}
                                    </el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="3">
                                <div>
                                    <span style="color: red;"
                                        v-if="application.businessType != '材料' && application.businessType != '废料'">*</span><span>合同</span>
                                </div>
                            </el-col>
                            <el-col :span="21">
                                <el-form-item prop="fileList" class="upLoad">
                                    <el-upload type="primary" v-model:file-list="fileList" :action="`api/files/upload/`"
                                        :headers="headers" :on-success="onSuccess" :on-change="handleChangePic"
                                        :show-file-list="false">
                                        <el-button>上传</el-button>
                                    </el-upload>
                                    <el-button style="margin-left: 20px;" @click="donwload"
                                        :disabled="application.file == ''">
                                        {{ application.fileName ? application.fileName : '下载' }}
                                    </el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <!-- <el-row
                            v-if="application.status != '' && (application.file != null && application.file != undefined)">
                            <el-col :span="3">
                                <div>
                                    <span>合同下载</span>
                                </div>
                            </el-col>
                            <el-col :span="21">
                                <el-form-item prop="fileList" class="upLoad">
                                    <el-button @click="donwload">下载</el-button>
                                </el-form-item>
                            </el-col>
                        </el-row> -->
                    </el-main>
                </el-container>
            </el-container>
        </el-form>
        <div class="foot"><span>说明：</span> 以上提供开票内容必须真实、完整、合法，并附有效合同。</div>
        <el-button type="primary" @click="cancel" v-if="use_store.operation == 1">返回</el-button>
        <el-button type="primary" @click="cancel" v-if="use_store.operation != 1">取消</el-button>
        <el-button type="primary" @click="draft(newform)" v-if="use_store.operation != 1"
            v-loading.fullscreen.lock="fullscreenLoading">草稿</el-button>
        <el-button type="primary" @click="save(newform)" v-if="use_store.operation != 1"
            v-loading.fullscreen.lock="fullscreenLoading">提交</el-button>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from "vue"
import { useRouter } from "vue-router";
import { math } from '@/utils/math';
import { saveNeiXiao, getCustomer, neixiao, findAllByName, neixiaoXiuGai, draftNeiXiao, deleteFile } from "@/utils/invoice";
import { useStore } from '@/stores/counter'
const use_store = useStore()
const router = useRouter();
const myDate = new Date();
const upload1 = ref()
const newform = ref()
const fullscreenLoading = ref(false)
const application = ref({
    type: '',
    status: '',
    parentId: null,
    file: '',
    applicationDepartment: JSON.parse(localStorage.getItem("user")).department,
    applyPerson: JSON.parse(localStorage.getItem("user")).username,
    applyTime: myDate.toLocaleDateString(),
    businessType: '',
    type: '',
    contractNo: '',
    planPayTime: null,
    customerName: '',
    customerAddress: '',
    customerPayNumber: '',
    orderNumber: '',
    fileName: '',
    list: [
        {
            commodityNo: '',
            specification: '',
            unit: '',
            amount: '',
            unitPriceIncludingTax: '',
            amountIncludingTax: 0
        },
        {
            commodityNo: '',
            specification: '',
            unit: '',
            amount: '',
            unitPriceIncludingTax: '',
            amountIncludingTax: 0
        },
        {
            commodityNo: '',
            specification: '',
            unit: '',
            amount: '',
            unitPriceIncludingTax: '',
            amountIncludingTax: 0
        }
    ]
})
const applicationOld = ref({
    type: '',
    status: '',
    parentId: null,
    applicationDepartment: JSON.parse(localStorage.getItem("user")).department,
    applyPerson: JSON.parse(localStorage.getItem("user")).username,
    applyTime: myDate.toLocaleDateString(),
    businessType: '',
    type: '',
    contractNo: '',
    planPayTime: null,
    customerName: '',
    customerAddress: '',
    customerPayNumber: '',
    orderNumber: '',
    list: [
        {
            commodityNo: '',
            specification: '',
            unit: '',
            amount: '',
            unitPriceIncludingTax: '',
            amountIncludingTax: 0
        },
        {
            commodityNo: '',
            specification: '',
            unit: '',
            amount: '',
            unitPriceIncludingTax: '',
            amountIncludingTax: 0
        },
        {
            commodityNo: '',
            specification: '',
            unit: '',
            amount: '',
            unitPriceIncludingTax: '',
            amountIncludingTax: 0
        }
    ]
})
const typeOptions = [
    {
        label: "配件",
        value: "配件"
    },
    {
        label: "箱类",
        value: "箱类"
    },
    {
        label: "材料",
        value: "材料"
    },
    {
        label: "废料",
        value: "废料"
    },
    {
        label: "零部件",
        value: "零部件"
    },
]

function upLoad(rule, value, callback) {
    if (fileList.value.length > 0 || application.value.businessType == '材料' || application.value.businessType == '废料' || (application.value.file != null && application.value.file != undefined)) {
        callback()
    } else {
        callback(new Error('请上传'))
    }
}

const rules = reactive({
    type: [
        { required: true, message: '请选择', trigger: 'change' },
    ],
    businessType: [
        { required: true, message: '请选择', trigger: 'change' },
    ],
    contractNo: [
        { required: true, message: '请填写', trigger: 'change' },
    ],
    contractNo1: [
        { required: false, message: '请填写', trigger: 'change' },
    ],
    planPayTime: [
        { required: true, message: '请选择', trigger: 'change' },
    ],
    customerName: [
        { required: true, message: '请选择', trigger: 'change' },
    ],
    paymentProvision: [
        { required: true, message: '请填写', trigger: 'change' },
    ],
    orderNumber: [
        { required: true, message: '请填写', trigger: 'change' },
    ],
    fileList: [
        { validator: upLoad, required: 'true', trigger: 'change' },
    ],
    // commodityNo: [
    //     { required: true, message: '请填写', trigger: 'change' },
    // ],
    // specification: [
    //     { required: true, message: '请填写', trigger: 'change' },
    // ],
    // unit: [
    //     { required: true, message: '请填写', trigger: 'change' },
    // ],
    // amount: [
    //     { required: true, message: '请填写', trigger: 'change' },
    // ],
    // unitPriceIncludingTax: [
    //     { required: true, message: '请填写', trigger: 'change' },
    // ],
})



const customerNameOptions = ref({})
const fileList = ref([])

onMounted(() => {
    getCustomer().then(res => {
        customerNameOptions.value = res.data;
    })
    if (use_store.operation != 3) {
        neixiao(use_store.id).then(res => {
            application.value = res.data.new
            if (use_store.operation == 4) {
                application.value.applicationDepartment = JSON.parse(localStorage.getItem("user")).department
                application.value.applyPerson = JSON.parse(localStorage.getItem("user")).username
                application.value.applyTime = myDate.toLocaleDateString()
            }
            if (res.data.new.parentId != null) {
                applicationOld.value = res.data.old
            }
        })
    }
})


function handleChangePic(file, fileList) {
    if (fileList.length > 1) {
        deleteFile(application.value.file)
        fileList.splice(0, 1);
    }
}

function onSuccess(res) {
    console.log(fileList);
    application.value.file = res.data.flag
    application.value.fileName = res.data.fileName
    console.log(application.value.file);

    newform.value.validateField("fileList")
}

function changeCustomerName() {
    customerNameOptions.value.map(item => {
        if (item.name == application.value.customerName) {
            application.value.customerAddress = item.address
            application.value.customerPayNumber = item.taxIdentificationNumber
            application.value.customerNameEasy = item.easyName
        }
    })
}

function donwload() {
    window.location.href = `/api/files/ty/${application.value.file}`
}

function searchCustomer(params) {
    findAllByName({ name: params }).then(res => {
        customerNameOptions.value = res.data
    })
}

function cancel() {
    router.push({
        path: use_store.path
    })
}

function draft(newform) {
    newform.validateField(["type"], (valid, falids) => {
        if (valid) {
            fullscreenLoading.value = true
            if (application.value.planPayTime != null) {
                application.value.planPayTime = new Date(application.value.planPayTime).toLocaleDateString()
            }
            if (use_store.operation == 4) {
                application.value.id = ''
                application.value.numberNo = ''
                application.value.createTime = ''
                application.value.status = ''
            }
            draftNeiXiao(application.value).then(res => {
                fullscreenLoading.value = false
                cancel()
            })
        }
    })
}

function save(newform) {
    newform.validate((valid, falids) => {
        if (valid) {
            fullscreenLoading.value = true
            if (application.value.status == '草稿') {
                application.value.id = ''
            }
            application.value.planPayTime = new Date(application.value.planPayTime).toLocaleDateString()
            if (application.value.status == '待开票' || application.value.status == '' || application.value.status == '草稿') {
                if (use_store.operation == 4) {
                    application.value.id = ''
                    application.value.numberNo = ''
                    application.value.createTime = ''
                    application.value.status = ''
                }
                saveNeiXiao(application.value).then(res => {
                    fullscreenLoading.value = false
                    cancel()
                })
            } else {
                neixiaoXiuGai(application.value).then(res => {
                    fullscreenLoading.value = false
                    cancel()
                })
            }
        }
    })
}

function changeMoney() {
    application.value.list.map(item => {
        item.amountIncludingTax = math.multiplyTwo(item.unitPriceIncludingTax, item.amount)
    })
}

</script>
<style lang="scss" scoped>
.body {
    margin: auto;
    margin-top: 15%;
    width: 1200px;
    text-align: center;

    .tit {
        font-size: 30px;
        font-weight: 700;
        margin-bottom: 20px;
    }

    .foot {
        text-align: left;
        margin-top: 20px;

        span {
            font-weight: 700;
        }
    }

    .el-form-item {
        width: 100%;
        margin-bottom: 0px;
    }
}

.el-header {
    height: auto;
    padding: 0px;
}

.el-main {
    padding: 0px;
    overflow: hidden;

    .title {
        .el-col {
            height: 34px;
        }
    }
}



.el-col {
    display: flex;
    flex-direction: column;
    justify-content: center;
    border: 1px solid black;
    text-align: center;
    padding: 0px;
}

.el-aside {
    text-align: center;
    width: auto;
    overflow: hidden;
}

// .el-col:nth-child(2n-1){
//     padding-left: ;
// }</style>
